package com.example.demo.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.Result;
import com.example.demo.dao.LoginUserMapper;
import com.example.demo.dao.UserFriendMapper;
import com.example.demo.entity.LoginUser;
import com.example.demo.entity.UserFriend;
import com.example.demo.entity.es.Msg;
import com.example.demo.exception.CodeEnums;
import com.example.demo.exception.SPIPException;
import com.example.demo.intf.UserInterface;
import com.example.demo.intf.WsInterface;
import com.example.demo.util.JwtUtil;
import com.google.common.base.Objects;
import com.google.common.collect.Maps;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author 88377761
 *
 */
@RestController
public class UserImpl implements UserInterface {
	@Autowired
	UserFriendMapper userFriendMapper;

	@Autowired
	LoginUserMapper loginUserMapper;

	@Autowired
	WsInterface ws;

	@Override
	@Transactional(rollbackFor = SPIPException.class)
	public Result addFriend(List<UserFriend> list) throws SPIPException {
		for (UserFriend userFriend : list) {
			userFriend.setOperator();
			if (!userFriendMapper.existsWithPrimaryKey(userFriend)) {
				userFriendMapper.insertSelective(userFriend);
				Msg msg = new Msg(userFriend.getUserId().toString(), userFriend.getFriendId().toString(), "嗨~");
				msg.setCmd(1);// 添加好友成功命令
				msg.setCreateTime(userFriend.getUpdateDttm().getTime());
				Map<String, Object> extras = Maps.newHashMap();
				extras.put("content", "我们已经是好友了，请开始聊天吧(系统提示)");
				extras.put("friendMobile", userFriend.getFriendMobile());
				extras.put("friendNickName", userFriend.getFriendNickName());
				msg.setExtras(extras);
				Result result = ws.sendToUser(msg);
				if (!Objects.equal(result.getCode(), "200")) {
					throw new SPIPException("500", result.getMsg());
				}
			}
		}
		return Result.getOk();
	}

	@Override
	public Result searchByMobile(String userMobile) throws SPIPException {
		Result result = new Result();
		if (StringUtils.isBlank(userMobile))
			throw new SPIPException(CodeEnums.USER_EEEOR_10003);
		LoginUser user = new LoginUser();
		user.setUserMobile(userMobile);
		LoginUser res = loginUserMapper.selectOne(user);
		if (res == null) {
			throw new SPIPException(CodeEnums.USER_EEEOR_10009);
		}
		UserFriend userFriend = new UserFriend();
		userFriend.setUserId(JwtUtil.getLUserId());
		userFriend.setFriendId(res.getUserId());
		boolean isFriend = userFriendMapper.existsWithPrimaryKey(userFriend);
		if (isFriend) {
			throw new SPIPException(CodeEnums.USER_EEEOR_10007);
		}
		result.setData(res);
		return result;
	}

	@Override
	public Result queryFriendsInc(UserFriend userFriend) throws SPIPException {
		if (userFriend.getUserId() == null)
			throw new SPIPException(CodeEnums.USER_EEEOR_10000);
		Example example = new Example(UserFriend.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", userFriend.getUserId());
		criteria.andGreaterThan("updateDttm", userFriend.getUpdateDttm());
		List<UserFriend> list = userFriendMapper.selectByExample(example);
		return Result.getResultOk(list);
	}

	@Override
	public Result update(LoginUser loginUser) throws SPIPException {
		if (StringUtils.isBlank(loginUser.getUserMobile()))
			throw new SPIPException(CodeEnums.USER_EEEOR_10003);
		Example example = new Example(LoginUser.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userMobile", loginUser.getUserMobile());
		loginUserMapper.updateByExampleSelective(loginUser, example);
		return Result.getOk();
	}

}
